<?php

// 广告管理

namespace app\console\controller;

use app\console\model\Managelog1;
use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Ad as AdModel;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Db;
use think\Debug;
class ManageLog extends Controller
{

    /**
     * 广告投放列表
     */
    public function index(Request $request, AdModel $ad)
    {
        // 获取参数


        $where1 = [];
        $where2 = [];
        $where1e = [];
        $title = Request::instance()->param('title');
        if(!empty($title)) $where1['username'] = ['eq',$title];
        if(!empty($title)) $where2['title'] = ['like','%'.$title.'%'];
//        $data = Db::name('managelog1 managelog')
//            ->join('manager','manager.id = managelog.manage_id')
//            ->join('auth_rule','managelog.url = auth_rule.name','left')
//            ->field('managelog.*,auth_rule.title module,manager.username')
//            ->order('managelog.update_time','desc')
//            ->where($where)
//            ->paginate(10);
        $w['id'] = ['gt',0];
//        ini_set('memory_limit', '120008M');
//        Debug::remark('begin');
//        $find = (new Managelog1())->order('id','asc')->limit(1000000)->select();
//        //$find = Db::name('Managelog1')->order('id','asc')->limit(1000000)->select();
//        Debug::remark('end');
//       echo 'A:' .Debug::getRangeTime('begin','end').'s<br/>';
//        echo Debug::getRangeMem('begin','end').'kb';
////        Debug::remark('begin');
////        $find = (new Managelog1())->order('id','desc')->limit(1)->find();
////        Debug::remark('end');
////        echo 'D:' .Debug::getRangeTime('begin','end').'s<br/';
//        return;
        if(!empty($title)) $where1e['username'] = ['eq','abd'];
        $ret = (new Managelog1())->hasWhere('Manager',$where1e)->with('Manager,authRule')->order('id','desc')->paginate(10,false,['query' => ['title' => $title]]);
       $ret1 = (new Managelog1())->hasWhere('Manager',$where1)->with('Manager,authRule')->order('id','desc')->paginate(10,false,['query' => ['title' => $title]]);
//        halt($ret->toArray());
        if(!$ret1->isEmpty()){
            $ret = $ret1;
        }else{
            $ret2 = (new Managelog1())->hasWhere('authRule',$where2)->with('Manager,authRule')->order('id','desc')->paginate(10,false,['query' => ['title' => $title]]);

            if(!$ret2->isEmpty()){
                $ret = $ret2;
            }
        }

        return $this->fetch('index', [
            'mate_title' => '操作日志',
            'data'       => $ret,
        ]);
    }
    public function index2(Request $request, AdModel $ad)
    {
        // 获取参数
        Debug::remark('begin');
        $find = (new Managelog1())->order('id','desc')->where('id',4896426)->find();
        Debug::remark('end');
        echo 'D:' .Debug::getRangeTime('begin','end').'s<br/>';
        return;

        $where = [];
        $title = Request::instance()->param('title');
        if(!empty($title)) $where['username'] = ['eq',$title];
//        $data = Db::name('managelog1 managelog')
//            ->join('manager','manager.id = managelog.manage_id')
//            ->join('auth_rule','managelog.url = auth_rule.name','left')
//            ->field('managelog.*,auth_rule.title module,manager.username')
//            ->order('managelog.update_time','desc')
//            ->where($where)
//            ->paginate(10);
        $w['id'] = ['gt',0];

        Debug::remark('begin');
        $find = (new Managelog1())->order('id','desc')->where('id',4896426)->find();
        Debug::remark('end');
        echo 'A:' .Debug::getRangeTime('begin','end').'s<br/>';
        Debug::remark('begin');
        $find = (new Managelog1())->order('id','asc')->where('id',4896426)->find();
        Debug::remark('end');
        echo 'D:' .Debug::getRangeTime('begin','end').'s<br/>';
        return;
        $ret = (new Managelog1())->hasWhere('authRule',$where)->with('Manager,authRule')->order('id','desc')->paginate(10);

        return $this->fetch('index', [
            'mate_title' => '操作日志',
            'data'       => $ret,
        ]);
    }


    /**
     * 新增广告
     */
    public function create(Request $request, AdModel $ad)
    {

        if ($request->instance()->isPost()) {


            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Ad');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $ad->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Ad/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('create', [
            "mate_title" => '广告投放添加',
            "data"       => ClassifyAlias::dimension(Cache::get('AdType')),
        ]);

    }


    /**
     * 编辑广告
     */
    public function update($id, Request $request, AdModel $ad)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Ad');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $ad->allowField(true)->save($data, ['id' => $id]);

            if ($dispose !== false) {
                win('操作成功', Url::build('Ad/index'));
            }

            fail('操作失败');

        }

        $item = AdModel::get($id);


        return $this->fetch('create', [
            "mate_title" => '广告投放编辑',
            'item'       => $item,
            "data"       => ClassifyAlias::dimension(Cache::get('AdType')),

        ]);
    }


    /**
     * 广告删除
     */
    public function destroy($id, AdModel $ad)
    {

        if ($ad->destroy($id)) {

            win('操作成功', Url::build('Ad/index'));
        }

        fail('操作失败');

    }


    /**
     * 广告排序
     */
    public function sorting(Request $request, AdModel $ad)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        foreach ($id as $key => $value) {
            $ad->where('id', $value)->update(['sort' => $sort[$key]]);
        }


        win('操作成功', Url::build('Ad/index'));
    }
}